from rest_framework import serializers
from .models import CourseCategory,Course,Teacher,CourseChapter,CourseSection


class CourseCategorySerialer(serializers.ModelSerializer):
    class Meta:
        model = CourseCategory
        fields = ['id', 'name']


class TeacherSerializer(serializers.ModelSerializer):
    class Meta:
        model = Teacher
        fields = ('id','name','role','title','signature','image','brief')

class CourseSerializer(serializers.ModelSerializer):
    # 子序列化
    teacher=TeacherSerializer(many=False)
    # course_type_name=serializers.CharField(source='get_course_type_display')
    # course_type_name=serializers.CharField(source='course_type_name')
    class Meta:
        model = Course
        fields = (
            'id',
            'name',
            'course_img',
            'brief',
            'attachment_path',
            'pub_sections',
            'price',
            'students',
            'period',
            'sections',
            'course_type_name',
            'level_name',
            'status_name',
            'teacher',
            'section_list',
            'course_category'
        )



## 课时序列化类
class CourseSectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseSection
        fields='__all__'


# 课程章节序列化类
class CourseChapterSerializer(serializers.ModelSerializer):
    # 子序列化,课时是多条，一定要写many=True
    coursesections=CourseSectionSerializer(many=True)
    class Meta:
        model = CourseChapter
        fields=['course','chapter','name','summary','pub_date','coursesections']